from openai import AsyncOpenAI, OpenAI
from config  import settings    

# 同步LLM调用
def call_llm(prompt):    
    api_key = settings.OPENAI_API_KEY
    if not api_key:
        raise ValueError("请设置OPENAI_API_KEY环境变量或在代码中提供API密钥")
        
    client = OpenAI(
        api_key=api_key,
        base_url=settings.OPENAI_BASE_URL
    )
    r = client.chat.completions.create(
        model=settings.OPENAI_MODEL,
        messages=[{"role": "user", "content": prompt}]
    )
    return r.choices[0].message.content

# 异步LLM调用 - 仅负责单个异步请求
async def call_llm_async(prompt):
    """异步调用LLM，不包含并发控制逻辑"""
    api_key = settings.OPENAI_API_KEY
    if not api_key:
        raise ValueError("请设置OPENAI_API_KEY环境变量或在代码中提供API密钥")
        
    client = AsyncOpenAI(
        api_key=api_key,
        base_url=settings.OPENAI_BASE_URL
    )
    r = await client.chat.completions.create(
        model=settings.OPENAI_MODEL,
        messages=[{"role": "user", "content": prompt}]
    )
    return r.choices[0].message.content
    
if __name__ == "__main__":
    prompt = "What is the meaning of life?"
    print(call_llm(prompt))
